Golang tutorial
Before create a new golang project, you need to install go firstly. If you didn’t install it, click here
A new golang project
Execute the following command, then you will get a new go project directory.
mkdir go-test
cd ./go-test
go mod init example.com/test
And then, we need to create a entrance of this project. Generally, we can create a file that names main.go to be a entrance of the go project.
package test
import "fmt"
func main() {
fmt.Println("Hello world")
}
where package test is the package name of this file.
Then execute go mod tidy and go run ., you will see Hello world in the terminal.
Call your code from another project
Firstly, you need to create a go project as a module, sudh that we can call it in the main project.
mkdir go-test
cd go-test
go mod init example.com/test
create a file which names main.go
package test
import "fmt"
func Hello(name string) string {
return fmt.Sprintf("Hello %s", name)
}
And then, we need to create the main go project.
mkdir go-main
cd go-main
go mod init example.com/main
Now, the construct of file directory is
– home
-- go-test
-- go-main
create an entrance file in the main project
package test
import "fmt"
import "example.com/test"
func main() {
fmt.Println(test.Hello("Go"));
}
Before tidy and run the main go project, we need to edit the example.com/test module to use your local example.com/test module. We need to run the following command:
// current path /home/go-main
go mod edit -replace example/test=../go-test
Then execute go mod tidy and go run ., the main project will print Hello Go
Return and error handle
package main
import (
"fmt"
"log"
"example.com/test"
)
func main() {
log.SetPrefix("example.com/test")
log.SetFlags(0)
str, err := test.Hello("")
if (err != nil) {
log.Fatal(err)
}
fmt.Println(str)
}
Math/rand
package main
import (
"fmt"
"math/rand"
)
func main() {
message := fmt.Sprintf(randomString(), "Go")
}
func randomString() string {
formats := []string{
"Hello %s",
"%s, hello",
"Hi, %s",
}
return formats[rand.Intn(len(formats))]
}
For iterator, make, and map
Documentation:
Code:
package main
import (
"fmt"
"log"
"math/rand"
"example.com/test"
)
func main() {
log.SetPrefix("example.com/test: ")
log.SetFlags(0)
names := []string{
"John",
"Jack",
"Tom",
}
messages, _ := Hellos(names)
fmt.Println(messages)
}
func Hellos(names []string) (map[string]string, error) {
messages := make(map[string]string)
for _, name := range names {
message, _ := test.Hello(name)
messages[message] = fmt.Sprintf(randomFormat(), message)
}
return messages, nil
}
func randomFormat() string {
formats := []string{
"Hi, %s",
"Hello, %s",
"Help, %s",
}
return formats[rand.Intn(len(formats))]
}
Unit test
package test
import (
"regexp"
"testing"
)
func TestHelloName(t *testing.T) {
name := "Tom"
want := regexp.MustCompile(`\b` + name + `\b`)
msg, err := Hello(name)
if !want.MatchString(msg) || err != nil {
t.Error(`invalid value`)
}
}
func TestHelloEmpty(t *testing.T) {
name := ""
msg, err := Hello(name)
if msg != "" || err == nil {
t.Error(`invalid empty value`)
}
}
Compile and install the application
-
go build – to compile the code into an executable
-
go list -f ‘{{.Target}}’ – the binaries are installed to /xx/gopher/bin path.
-
go env -w GOBIN=/path/to/your/bin – change the install target by setting the GOBIN variable using the go env command
-
go install – to compile and install the package
-
go get – to get go packages by using this command